package com.cql.configuration;

import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.server.RequestPredicates;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerResponse;

@Configuration
public class GateWayConfiguration {

    //路由断言,外界通过访问http://localhost:9000/test，直接返回结果
    @Bean
    public RouterFunction<ServerResponse> testFunRouterFunction() {
        RouterFunction<ServerResponse> route = RouterFunctions.route(
                RequestPredicates.path("/test"), request -> ServerResponse.ok().
                        body(BodyInserters.fromObject("I am testINg"))

        );
        return route;
    }

    //过滤器,外界访问http://localhost:9000/cql/rest/145可以被跳转到正确的服务器上
    @Bean
    public RouteLocator customRouterLactor(RouteLocatorBuilder builder) {
        return builder.routes().route(r -> r.path("/haha/**").uri("lb://eureka-hystrix-service"))
                .build();
    }
}
